import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_base_framework/flutter_base_framework.dart';
import 'package:flutter_common_utils/flutter_common_utils.dart';
import 'package:get/get.dart';

HistoryRouteObserver routeRecord = HistoryRouteObserver();

///记录路由历史
class HistoryRouteObserver extends RouteObserver<PageRoute> {
  List<Route<dynamic>> history = <Route<dynamic>>[];
  Route<dynamic>? curRoute;

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPop(route, previousRoute);
    curRoute = previousRoute;
    history.remove(route);
    //调用Navigator.of(context).pop() 出栈时回调
  }

  @override
  void didPush(Route route, Route? previousRoute) {
    super.didPush(route, previousRoute);
    curRoute = route;
    history.add(route);
    //调用Navigator.of(context).push(Route()) 进栈时回调
  }

  @override
  void didRemove(Route route, Route? previousRoute) {
    super.didRemove(route, previousRoute);
    history.remove(route);
    //调用Navigator.of(context).removeRoute(Route()) 移除某个路由回调
  }

  @override
  void didReplace({Route? newRoute, Route? oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    curRoute = newRoute;
    if (oldRoute != null) {
      history.remove(oldRoute);
    }
    if (newRoute != null) {
      history.add(newRoute);
    }
    //调用Navigator.of(context).replace( oldRoute:Route("old"),newRoute:Route("new")) 替换路由时回调
  }
}

//监听实现OnResume和OnPause
// extension BaseWidgetStateRouteObserverExtension on BaseWidgetState {
//   // 监听OnResume和OnPause
//   void startResumeAndPause() {
//     // 订阅路由变化
//     final route = ModalRoute.of(context);
//     if (route is PageRoute) {
//       routeHistoryObserver.subscribe(this, route);
//     }
//   }
// }
mixin OnPauseOnResumeRouteAwareMixin<T extends BasePage> on BaseState<T> {
  late final OnPauseOnResumeRouteAware _onPauseOnResumeRouteAware = OnPauseOnResumeRouteAware(onPause: _onPause, onResume: _onResume);
  late PageRoute _route;
  bool isPaused = true;

  void _onPause() {
    if (!isPaused) {
      isPaused = true;
      onPause();
    }
  }

  void _onResume() {
    if (isPaused) {
      isPaused = false;
      onResume();
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 订阅路由变化
    _route = ModalRoute.of(context) as PageRoute;
    routeRecord.subscribe(_onPauseOnResumeRouteAware, _route);
  }

  // 应用生命周期变化回调
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    // 处理生命周期变化
    if (state == AppLifecycleState.resumed) {
    } else if (state == AppLifecycleState.paused) {}
    switch (state) {
      case AppLifecycleState.resumed:
        //是否是当前页面
        if (_route.isCurrent) {
          _onResume();
        }
        LogUtils.i("App 回到前台");
        break;
      case AppLifecycleState.inactive:
        LogUtils.i("App 处于非活动状态");
        break;
      case AppLifecycleState.paused:
        _onPause(); // 应用进入后台
        LogUtils.i("App 进入后台");
        break;
      case AppLifecycleState.detached:
        LogUtils.i("App 被销毁");
        break;
      case AppLifecycleState.hidden:
    }
  }

  @override
  void dispose() {
    super.dispose();
    routeRecord.unsubscribe(_onPauseOnResumeRouteAware);
  }

  void onPause() {
    LogUtils.i("onPause :$pageName");
  }

  void onResume() {
    LogUtils.i("onResume :$pageName");
  }
}

class OnPauseOnResumeRouteAware extends RouteAware {
  VoidCallback onPause;
  VoidCallback onResume;

  OnPauseOnResumeRouteAware({required this.onPause, required this.onResume});

  // 当页面被覆盖（如
  // 当页面被新页面覆盖（如跳转）时触发
  @override
  void didPushNext() {
    onPause();
  }

  //当当前路线被推出时调用
  @override
  void didPush() {
    super.didPush();
    onResume();
  }

  // 当页面重新回到栈顶（如返回）时触发
  @override
  void didPopNext() {
    onResume();
  }
}
